package ctgu.yao.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ctgu.yao.cloud.dto.ResultDTO;
import ctgu.yao.cloud.form.RuleForm;
import ctgu.yao.cloud.po.Companyuser;
import ctgu.yao.cloud.mapper.CompanyuserMapper;
import ctgu.yao.cloud.po.Resume;
import ctgu.yao.cloud.service.CompanyuserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ctgu.yao.cloud.form.RuleForm2;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Y
 * @since 2022-08-16
 */
@Service
public class CompanyuserServiceImpl extends ServiceImpl<CompanyuserMapper, Companyuser> implements CompanyuserService {
    @Autowired
    private CompanyuserMapper companyuserMapper;
    //登录
    @Override
    public ResultDTO login(RuleForm ruleForm) {
        QueryWrapper<Companyuser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",ruleForm.getUsername());
        Companyuser companyuser = this.companyuserMapper.selectOne(queryWrapper);
        ResultDTO resultDTO = new ResultDTO<>();
        if(companyuser == null){
            resultDTO.setCode(-1);
        }else{
            //判断密码是否正确
            if(!companyuser.getPassword().equals(ruleForm.getPassword())) {
                resultDTO.setCode(-2);
            }else {
                resultDTO.setCode(0);
                resultDTO.setData(companyuser);
            }
        }
        return  resultDTO;
    }
    //注册
    @Override
    public ResultDTO register(RuleForm2 ruleForm) {
        QueryWrapper<Companyuser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",ruleForm.getUsername());
        Companyuser companyuser = this.companyuserMapper.selectOne(queryWrapper);
        ResultDTO resultDTO = new ResultDTO<>();
        if(companyuser==null){
            resultDTO.setCode(0);
            Date date = new Date();
            Companyuser companyuser1 = new Companyuser(ruleForm.getUsername(),ruleForm.getPassword(),
                    ruleForm.getName(),ruleForm.getPhone(),ruleForm.getSex(),date.getYear()-ruleForm.getDate().getYear());
            this.companyuserMapper.insert(companyuser1);
        }else{
            resultDTO.setCode(-1);
        }
        return resultDTO;
    }
}
